package com.snail.billing.page;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.android.net.HttpApp;
import com.android.net.HttpResult;
import com.android.net.HttpSession;
import com.android.net.OnHttpResultListener;
import com.android.util.AlertUtil;
import com.android.util.ResUtil;
import com.snail.billing.Billing;
import com.snail.billing.DataCache;
import com.snail.billing.Resource;
import com.snail.billing.data.Account;
import com.snail.billing.data.AccountManager;
import com.snail.billing.os.DialogPage;
import com.snail.billing.session.RegisterRandomSession;

public class LaunchPage extends DialogPage implements View.OnClickListener, OnHttpResultListener {

	private View buttonClose;

	private EditText inputAccount;
	private EditText inputPwd;
	private View buttonForgetPwd;
	private View buttonLogin;
	private View buttonRegister;
	private View buttonRandomRegister;

	private HttpApp httpApp;

	private RegisterRandomSession registerRandomSession;

	private String accountString, pwdString;

	@Override
	public int createLayoutId() {
		return ResUtil.getLayoutId(Resource.layout.snailbilling_launch_activity);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		buttonClose = findViewById(ResUtil.getViewId(Resource.id.snailbilling_title_button_close));
		buttonClose.setOnClickListener(this);
		//
		inputAccount = (EditText) findViewById(ResUtil.getViewId(Resource.id.snailbilling_launch_input_account));
		inputPwd = (EditText) findViewById(ResUtil.getViewId(Resource.id.snailbilling_launch_input_pwd));
		buttonForgetPwd = findViewById(ResUtil.getViewId(Resource.id.snailbilling_launch_button_forget_pwd));
		buttonLogin = findViewById(ResUtil.getViewId(Resource.id.snailbilling_launch_button_login));
		buttonRegister = findViewById(ResUtil.getViewId(Resource.id.snailbilling_launch_button_register));
		buttonRandomRegister = findViewById(ResUtil.getViewId(Resource.id.snailbilling_launch_button_random_register));
		buttonForgetPwd.setOnClickListener(this);
		buttonLogin.setOnClickListener(this);
		buttonRegister.setOnClickListener(this);
		buttonRandomRegister.setOnClickListener(this);
		//
		httpApp = new HttpApp(getContext());
		httpApp.setOnHttpResultListener(this);
	}

	@Override
	public void onClick(View v) {
		if (v.equals(buttonClose)) {
			getPageManager().finish();
		} else if (v.equals(buttonForgetPwd)) {
			getPageManager().forward(ForgetPage.class);
		} else if (v.equals(buttonLogin)) {
			accountString = inputAccount.getText().toString();
			if (TextUtils.isEmpty(accountString)) {
				AlertUtil.show(getContext(), ResUtil.getString(Resource.string.snailbilling_launch_input_account));
				return;
			}
			pwdString = inputPwd.getText().toString();
			if (TextUtils.isEmpty(pwdString)) {
				AlertUtil.show(getContext(), ResUtil.getString(Resource.string.snailbilling_launch_input_pwd));
				return;
			}
			getPageManager().finish();
			DataCache.getInstance().importParams.pageClass = DialogChangePage.class;
			Intent intent = new Intent(getContext(), Billing.class);
			intent.putExtra(DialogChangePage.ACCOUNT, accountString);
			intent.putExtra(DialogChangePage.PWD, pwdString);
			intent.putExtra(DialogChangePage.TYPE, Account.TYPE_COMMON);
			getContext().startActivity(intent);
		} else if (v.equals(buttonRegister)) {
			getPageManager().forward(RegisterPage.class);
		} else if (v.equals(buttonRandomRegister)) {
			registerRandomSession = new RegisterRandomSession();
			httpApp.request(registerRandomSession);
		}
	}

	@Override
	public void onHttpResult(HttpResult httpResult) {
		if (httpResult.isSucceed()) {
			HttpSession httpSession = httpResult.getHttpSession();
			if (httpSession.equals(registerRandomSession)) {
				RegisterRandomSession.JsonData jsonData = new RegisterRandomSession.JsonData((String) httpSession.getResponseData());
				if (jsonData.getCode() == 1) {
					String accountString = jsonData.getPassport();
					String pwdString = jsonData.getPasswd();
					Account currentAccount = new Account();
					currentAccount.setAccount(accountString);
					currentAccount.setPwd(pwdString);
					AccountManager.setRandomAccount(currentAccount);
					getPageManager().finish();
					DataCache.getInstance().importParams.pageClass = DialogUpdatePage.class;
					getContext().startActivity(new Intent(getContext(), Billing.class));
				} else {
					Toast.makeText(getContext(), jsonData.getMessage(), Toast.LENGTH_SHORT).show();
				}
			}
		}
	}

}
